package org.csu.agrisupply.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.csu.agrisupply.api.common.CommonResponse;
import org.csu.agrisupply.api.entity.Form;
import org.csu.agrisupply.api.entity.FormOffer;
import org.csu.agrisupply.api.entity.Item;
import org.csu.agrisupply.api.entity.User;
import org.csu.agrisupply.api.persistence.FormMapper;

import org.csu.agrisupply.api.persistence.FormOfferMapper;
import org.csu.agrisupply.api.persistence.ItemMapper;
import org.csu.agrisupply.api.persistence.UserMapper;
import org.csu.agrisupply.api.service.FormService;
import org.csu.agrisupply.api.vo.FormVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("FormService")
public class FormServiceImpl implements FormService {
    @Autowired
    private FormMapper formMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private FormOfferMapper formOfferMapper;


    @Override
    public CommonResponse<List<FormVo>> getFormList() {
        List<Form> formList = formMapper.selectList(null);
        if(formList.isEmpty()){
            return CommonResponse.createForSuccessMessages("没有分类信息");
        }
        QueryWrapper<Form> wrapper = new QueryWrapper<Form>();
        wrapper.eq("status",0);
        List<Form> formListresult = formMapper.selectList(wrapper);
        if(formListresult.isEmpty()){
            return CommonResponse.createForSuccessMessages("所有采购单已完成或无有效采购单");
        }
        List<FormVo> formVoList = new ArrayList<>();
        for (Form i : formList){
            Item item = itemMapper.selectById(i.getItemId());
            if(item == null)
            {
                return CommonResponse.createForSuccessMessages("没有formId：" + i.getFormId() + "的itemId对应的item");
            }
            FormVo formVo = FormtoFormVo(i,item);
            formVoList.add(formVo);
        }
        return CommonResponse.createForSuccess(formVoList);
    }

    @Override
    public CommonResponse<FormVo> getForm(String formId) {
        Form form = formMapper.selectById(formId);
        if(form == null)
        {
            return CommonResponse.createForSuccessMessages("没有该ID的form");
        }
        Item item = itemMapper.selectById(form.getItemId());
        if(item == null)
        {
            return CommonResponse.createForSuccessMessages("没有该itemId的item");
        }
        FormVo formVo = FormtoFormVo(form,item);
        return  CommonResponse.createForSuccess(formVo);
    }

    @Override
    public CommonResponse<Form> createForm(String userId, Form form) {
        User user = userMapper.selectById(userId);
        if(user == null)
        {
            return CommonResponse.createForSuccessMessages("没有该ID的user");
        }
        form.setFormId(getMax());
        form.setUserId(userId);
        form.setOffercount(0);
        form.setStatus(0);
        form.setCount(form.getTotalcount());
        formMapper.insert(form);
        return  CommonResponse.createForSuccess("发布成功",form);
    }

    @Override
    public CommonResponse<List<FormVo>> getFormbyUserId(String userId) {
        User user = userMapper.selectById(userId);
        if(user == null)
        {
            return CommonResponse.createForSuccessMessages("没有该ID的user");
        }
        QueryWrapper<Form> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId",userId);
        List<Form> formListresult = formMapper.selectList(queryWrapper);
        if(formListresult.isEmpty()){
            return CommonResponse.createForSuccessMessages("该用户没有发布过采购单");
        }
        List<FormVo> formVoList = new ArrayList<>();
        for(Form i : formListresult){
            Item item = itemMapper.selectById(i.getItemId());
            if(item == null)
            {
                return CommonResponse.createForSuccessMessages("没有该itemId的item");
            }
            FormVo formVo = FormtoFormVo(i,item);
            formVoList.add(formVo);
        }
        return CommonResponse.createForSuccess(formVoList);
    }


    @Override
    public CommonResponse<List<FormVo>> getFormFilter(String userId) {
        User user = userMapper.selectById(userId);
        if(user == null)
        {
            return CommonResponse.createForSuccessMessages("没有该ID的user");
        }
        QueryWrapper<FormOffer> formOfferQueryWrapper = new QueryWrapper<>();
        formOfferQueryWrapper.eq("userId",userId);
        List<FormOffer> formOfferList = formOfferMapper.selectList(formOfferQueryWrapper);
        QueryWrapper<Form> formQueryWrapper = new QueryWrapper<>();
        formQueryWrapper.ne("status",1);
        for(FormOffer item : formOfferList){
            formQueryWrapper.ne("formId",item.getFormId());
        }
        List<Form> formList = formMapper.selectList(formQueryWrapper);
        List<FormVo> formVoList = new ArrayList<>();
        for(Form item : formList){
            Item item1 = itemMapper.selectById(item.getItemId());
            formVoList.add(FormtoFormVo(item,item1));
        }
        return CommonResponse.createForSuccess(formVoList);
    }




    public String getMax(){
        QueryWrapper<Form> wrapper = new QueryWrapper<>();
        wrapper.select("formId");
        List<Object> formList = formMapper.selectObjs(wrapper);
        int Max = 0;
        if(!formList.isEmpty()){
            Max = Integer.parseInt(formList.get(0).toString());
            for(Object item : formList){
                int num = Integer.parseInt(item.toString());
                if (num >= Max)




                    Max = num;
            }
        }
        return Integer.toString(Max + 1);
    }
    public FormVo FormtoFormVo(Form form,Item item){
        FormVo formVo = new FormVo();
        formVo.setFormId(form.getFormId());
        formVo.setUserId(form.getUserId());
        formVo.setCatId(form.getCatId());
        formVo.setItemId(form.getItemId());
        formVo.setCycle(form.getCycle());
        formVo.setCount(form.getCount());
        formVo.setReceiptAddress(form.getReceiptAddress());
        formVo.setExpectedAddress(form.getExpectedAddress());
        formVo.setPrice(form.getPrice());
        formVo.setDescription(form.getDescription());
        formVo.setOffercount(form.getOffercount());
        formVo.setStatus(form.getStatus());
        formVo.setUnits(form.getUnits());
        formVo.setDdl(form.getDdl());
        formVo.setTotalcount(form.getTotalcount());
        //item中的form属性
        formVo.setItemName(item.getItemName());
        formVo.setCategoryId(item.getCategoryId());
        formVo.setImage1(item.getImage1());
        formVo.setImage2(item.getImage2());
        return formVo;
    }
}
